package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.dao.BspsManagerInfoMapper;
import cc.rengu.igas.bsps.common.dao.EventLogMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsManagerInfoMapperImpl;
import cc.rengu.igas.bsps.common.dao.impl.EventLogMapperImpl;
import cc.rengu.igas.bsps.common.entity.EventLog;
import cc.rengu.igas.bsps.common.entity.ManagerInfo;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.bean.WorkEvenLogInfoBean;
import cc.rengu.igas.bsps.facade.request.EventProcLogQueryRequest;
import cc.rengu.igas.bsps.facade.response.EventProcLogQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 工单操作日志查询服务
 * Created by 王成 on 2018/10/26.
 *
 * @author zhangxuran
 * @version 1.0
 * @date 2020/04/07
 */
public class EventProcLogQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        EventProcLogQueryRequest eventProcLogQueryRequest = new EventProcLogQueryRequest();
        ConvertUtil.convertOutput(eventProcLogQueryRequest);
        return eventProcLogQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        EventProcLogQueryRequest eventProcLogQueryRequest = (EventProcLogQueryRequest) request;
        /*获取请求报文参数:法人行，用户标识，事件ID*/
        String instId = eventProcLogQueryRequest.getHeader().getInstId();
        String userId = eventProcLogQueryRequest.getUserId();
        String eventId = eventProcLogQueryRequest.getEventId();
        /* 参数合法性校验 */
        if (StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(eventId) || StringUtil.isEmptyOrNull(instId)) {
            rglog.error("用户标识<{}>及事件编号<{}>不能为空!", userId, eventId);
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        /* 根据事件标识查询操作日志信息表 */
        List<WorkEvenLogInfoBean> eventLogInfoList = new ArrayList<>();
        EventLogMapper eventLogMapper = new EventLogMapperImpl();
        List<EventLog> eventLogList = eventLogMapper.selectEventLogInfoByEventId(instId, eventId);
        if (null != eventLogList && !eventLogList.isEmpty()) {
            eventLogList.forEach(item -> convEventLogInfo(item, eventLogInfoList));
        }
        /* 组装应答报文 */
        EventProcLogQueryResponse eventProcLogQueryResponse = new EventProcLogQueryResponse();
        eventProcLogQueryResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        eventProcLogQueryResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        eventProcLogQueryResponse.setWorkEvenLogInfoList(eventLogInfoList);
        BizResponse<EventProcLogQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(eventProcLogQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        EventProcLogQueryResponse eventProcLogQueryResponse = (EventProcLogQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(eventProcLogQueryResponse);
    }

    /**
     * 工单操作日志信息转换
     *
     * @param eventLog         工单操作流水
     * @param eventLogInfoList 工单操作日志信息
     */
    protected void convEventLogInfo(EventLog eventLog, List<WorkEvenLogInfoBean> eventLogInfoList) {
        WorkEvenLogInfoBean workEvenLogInfo = new WorkEvenLogInfoBean();
        workEvenLogInfo.setEventId(eventLog.getEventId());
        workEvenLogInfo.setEventStatus(eventLog.getEventStatus());
        workEvenLogInfo.setEventProcDesc(eventLog.getEventProcDesc());
        workEvenLogInfo.setOprId(eventLog.getOprId());
        workEvenLogInfo.setOprDate(eventLog.getOprDate());
        workEvenLogInfo.setOprTime(eventLog.getOprTime());

        BspsManagerInfoMapper bspsManagerInfoMapper = new BspsManagerInfoMapperImpl();
        try {
            ManagerInfo managerInfo = bspsManagerInfoMapper.selectBspsManagerNameByManagerId(eventLog.getInstId(), eventLog.getOprId());
            if (null != managerInfo && !StringUtil.isEmptyOrNull(managerInfo.getManagerName())) {
                workEvenLogInfo.setOprName(managerInfo.getManagerName());
            }
        } catch (Exception e) {
            rglog.error("拓展经理姓名查询失败，多法人标识<{}>，拓展经理工号<{}>", eventLog.getInstId(), eventLog.getOprId());
        }
        eventLogInfoList.add(workEvenLogInfo);
    }
}
